package com.fingard.dsp.bank.directbank.wrcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class WRCB01Balance extends WRCB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            WriteBankLogLn("进入余额查询处理类 ");
            int detailNum = 1;
            while (detailNum <= p_req7506.detailCount) {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                int actNum = 0;//账户个数
                xmlWriter.writeStartElement("Message");
                xmlWriter.writeStartElement("Head");
                xmlWriter.writeElementString("TrsCode", "B2EActBalQry");//交易码
                xmlWriter.writeElementString("ERPTrsTimestamp", p_req7506.reqHeader.transTime);//客户端时间
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeStartElement("List");
                while (p_req7506.readRecord()) {
                    detailNum++;
                    actNum++;
                    xmlWriter.writeStartElement("Map");
                    xmlWriter.writeElementString("AcNo", p_req7506.readedDetail.actNum);
                    xmlWriter.writeEndElement();
                    if (actNum >= 10) {
                        break;
                    }
                }
                xmlWriter.writeEndDocument();

                RetAts7506BalQry errRetAts = new RetAts7506BalQry(p_req7506.reqHeader);//如果异常则返回该结果给ATS
                errRetAts.writeStart();//写返回ATS的统一头部信息
                errRetAts.writeRespCode("-1");

                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String returnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if (returnCode != null && "000000".equals(returnCode)) {
                            while (tmpBkReader.read("/Message/Body/List/Map")) {
                                String ReturnCode = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                                String ReturnMsg = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                if ("000000".equals(ReturnCode)) {
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.respInfo = "余额查询成功！";
                                    tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcNo");
                                    tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Currency"));//币种
                                    tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Balance");//当前余额
                                    tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");//可用余额
                                    retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                                } else {
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.respInfo = "余额查询失败:" + ReturnMsg;
                                    retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                                }
                            }
                        } else {
                            errRetAts.writeRespInfo("错误代码：" + returnCode + ";错误信息：" + returnMsg);
                            errRetAts.writeEnd();
                            return errRetAts;
                        }
                    } else {
                        errRetAts.writeRespInfo(tmpSbMsg.toString());
                        errRetAts.writeEnd();
                        return errRetAts;
                    }
                } else {
                    errRetAts.writeRespInfo(tmpRetBkStr[1]);
                    errRetAts.writeEnd();
                    return errRetAts;
                }
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("余额查询异常!");
            e.printStackTrace();
            WriteBankLogLn("余额查询异常：" + e.getMessage());
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
